<script setup>
import {inject, ref, watchEffect} from 'vue';

const group = inject('group')

const props = defineProps({
  size:{
    type:String,
    default:'mini'
  },
  value: {
    type: [Number, String],
    default: ''
  },
  modelValue: {
    type: [Object, Boolean],
    default: () => {
      return {}
    }
  },
  disabled:{
    type:Boolean,
    default:false
  },
  defaultCheck:{
    type:Boolean,
    default:false
  }
})

const emits = defineEmits(['update:modelValue','change'])

const checked = ref(false)

watchEffect(() => {
  if (!group) {
    checked.value = !!props.defaultCheck
    return;
  }

  let values = group.getItems() || [];
  checked.value = values.includes(props.value)
  console.log(checked.value)
})

const emitItem = function () {
  console.log("click")

  if(props.disabled){
    return;
  }
  checked.value = !checked.value
  let emit = {checked: checked.value, value: props.value}
  if (group) {
    group[checked.value ? 'addItem' : 'deleteItem'](emit);
    return;
  }
  emits('update:modelValue', emit);
  emits('change', emit);
}

</script>

<template>
  <div class="custom-checkbox">
    <label class="checkbox__label" @click="emitItem">
      <checkbox :class="['checkbox__input']" :value="value" :checked="checked" :disabled="props.disabled"></checkbox>
      <div :class="['checkbox__inner',checked?'checked':'',`el-c-checkbox-${props.size}`]">
        <div class="checkbox__inner-icon"></div>
      </div>
      <div class="checkbox__text">
        <slot></slot>
      </div>
    </label>
  </div>
</template>

<style scoped lang="scss">
.custom-checkbox {

  .checkbox__label {
    display: flex;
    flex-direction: row;
    align-items: center;

    .checkbox__text {
      margin: 0 5rpx;
    }


    .checkbox__input {
      display: none;

      &[checked] {
        & + .checkbox__inner {
          background-color: #2979ff;
          border-color: #2979ff;

          .checkbox__inner-icon {
            opacity: 1;
          }
        }
      }

      &[disabled] {
        & + .checkbox__inner {
          background-color: #edf2fc;
          border-color: #dcdfe6;

          .checkbox__inner-icon {
            opacity: 0;
          }
        }
      }
    }



    .checkbox__inner {
      /* #ifndef APP-NVUE */
      flex-shrink: 0;
      box-sizing: border-box;
      /* #endif */
      position: relative;
      width: 16px;
      height: 16px;
      border: 1px solid #ddd;
      border-radius: 4px;
      background-color: #fff;
      z-index: 1;

      &.el-c-checkbox{

        &-large{
          width: 32px;
          height: 32px;

          .checkbox__inner-icon{
            top: 4px;
            left: 9px;
            height: 16px;
            width: 10px;
          }
        }
      }

      &.checked{
        background-color: #2979ff;
        border-color: #2979ff;

        .checkbox__inner-icon {
          opacity: 1;
        }
      }

      .checkbox__inner-icon {
        position: absolute;
        /* #ifdef APP-NVUE */
        top: 2px;
        /* #endif */
        /* #ifndef APP-NVUE */
        top: 1px;
        /* #endif */
        left: 5px;
        height: 8px;
        width: 4px;
        border-right-width: 1px;
        border-right-color: #fff;
        border-right-style: solid;
        border-bottom-width: 1px;
        border-bottom-color: #fff;
        border-bottom-style: solid;
        opacity: 0;
        transform-origin: center;
        transform: rotate(40deg);
      }
    }
  }
}

</style>
